﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using System.Xml.Linq;


namespace Trotware.Model
{
    public class Master_Model
    {
        String varid;
        String vardestinataire;
        String varadresse;
        String varetat;
        String vartype;

        Colis Colis = new Colis();
        private ManualResetEvent allDone = new ManualResetEvent(false);
        XDocument doc = new XDocument(
                   new XElement("args",
                       new XElement("userId", "data1"),
                       new XElement("password", "data2")
                   )
               );


        public void GetListeColis()
        {


            RequestColis();



        }

        //Test fct
        public void request_SRV(String rep)
        {

            String dataTest = "<ns0:list xmlns:ns0=\"http://xml.netbeans.org/schema/internalSchema\"><ns1:packageListResponse xmlns:ns1=\"http://webservice/\"><return xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:msgns=\"http://webservice/\" xmlns:ns2=\"http://webservice/\" xmlns:ns3=\"http://j2ee.netbeans.org/wsdl/TrotwareBPEL/src/integer\"><address><city>City</city><country>Coutrny</country><street>34, rue de Flowers</street><zip>1234</zip></address><clientName>Client 1</clientName><id>AABBCC</id><state>0</state><type>0</type></return><return xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:msgns=\"http://webservice/\" xmlns:ns2=\"http://webservice/\" xmlns:ns3=\"http://j2ee.netbeans.org/wsdl/TrotwareBPEL/src/integer\"><address><city>City</city><country>Coutrny</country><street>34, rue de Flowers</street><zip>1234</zip></address><clientName>Client 2</clientName><id>AABBCC</id><state>0</state><type>0</type></return></ns1:packageListResponse></ns0:list>";
            XDocument xml = XDocument.Parse(rep);
            XDocument xml2 = XDocument.Parse(xml.Root.FirstNode.ToString());
            XDocument xml3 = XDocument.Parse(xml2.Root.FirstNode.ToString());
            XName name = xml3.Root.Name;
            XDocument xml4;

            // 1er node
            foreach (var xe in xml2.Descendants(name))
            {

                var test = xe.ToString();

                xml4 = XDocument.Parse(test);


                varid = xml4.Root.Element("id").Value;
                vardestinataire = xml4.Root.Element("address").Element("street").Value + " " + xml4.Root.Element("address").Element("zip").Value + " " + xml4.Root.Element("address").Element("city").Value;
                varadresse = xml4.Root.Element("clientName").Value;
                varetat = xml4.Root.Element("state").Value;
                vartype = xml4.Root.Element("type").Value;

                //(Application.Current as App).liste.Add(genColis(varid, vardestinataire, varadresse, varetat, vartype));


            }
        }

        public void RequestColis()
        {


            HttpWebRequest requete = (HttpWebRequest)HttpWebRequest.Create("http://10.10.3.188:9696/packageList");
            requete.Method = "POST";
            requete.ContentType = "application/xml";
            //requete.Accept = "application/json";


            requete.BeginGetRequestStream(DebutReponse, requete);
        }

        private void DebutReponse(IAsyncResult resultatAsynchrone)
        {
            HttpWebRequest requete = (HttpWebRequest)resultatAsynchrone.AsyncState;
            Stream postStream = requete.EndGetRequestStream(resultatAsynchrone);
            string donneesAEnvoyer = "<ns0:args xmlns:ns0='http://xml.netbeans.org/schema/internalSchema'><ns0:userId>plop</ns0:userId><ns0:password>pap</ns0:password></ns0:args>";

            byte[] tableau = Encoding.UTF8.GetBytes(donneesAEnvoyer);
            postStream.Write(tableau, 0, donneesAEnvoyer.Length);
            postStream.Close();
            requete.BeginGetResponse(FinReponse, requete);
        }

        private void FinReponse(IAsyncResult resultatAsynchrone)
        {
            HttpWebRequest requete = (HttpWebRequest)resultatAsynchrone.AsyncState;
            WebResponse webResponse = requete.EndGetResponse(resultatAsynchrone);
            Stream stream = webResponse.GetResponseStream();

            StreamReader streamReader = new StreamReader(stream);
            string reponse = streamReader.ReadToEnd();
            stream.Close();
            streamReader.Close();
            webResponse.Close();

            request_SRV(reponse);


        }

        private Colis genColis(String id, String destinataire, String adresse, String etat, String type)
        {
            Colis colis = new Colis();
            colis.id = id;
            colis.destinataire = destinataire;
            colis.Adresse = adresse;
            colis.Etat_Colis = etat;
            colis.Type_Colis = type;


            return colis;
        }




    }
}
